#include <string.h>
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
//派生类对象之间的复制控制
//基类中定义好了复制控制函数，但是派生类中没有定义复制控制函数
//此时进行派生类对象的复制控制时，是能够正确处理的，也就是会
//调用基类的复制控制函数
class Base{
public:
    Base(const char * pstr)
    :_base(new char[strlen(pstr) + 1]())
    {
        strcpy(_base, pstr);
    }

    Base(const Base & rhs)
    :_base(new char[strlen(rhs._base) + 1]())
    {
        strcpy(_base, rhs._base);
    }
    //赋值运算符
    Base & operator=(const Base & rhs){
        if(this != &rhs){
            delete [] _base;
            _base = new char[strlen(rhs._base) + 1]();
            strcpy(_base, rhs._base);
        }
        return *this;
    }
    ~Base(){
        if(_base){
            delete [] _base;
            _base = nullptr;
        }
    }

protected:
    char * _base;
};
//派生类中没有任何的数据成员，数据成员此时位于基类中的
class Derived : public Base{
public:
    Derived(const char * base)
    :Base(base)
    {
        cout << "Derived(const char *)" << endl;
    }
    friend ostream & operator<<(ostream & os, const Derived & rhs);

};
ostream & operator<<(ostream & os, const Derived & rhs){
    os << rhs._base;
    return os;
}


void test(){
    Derived d1("hello");
    Derived d2 = d1;
    cout << d1 << ":" << d2 << endl;
    cout << "================" << endl;
    Derived d3 = "world";
    Derived d4 = "hello,c++";
    d3 = d4;
    cout << d3 << ":::::" << d4 << endl;
}

int main()
{
    test();
    return 0;
}

